package io.renren.modules.jxgk.service.impl;

import io.renren.modules.jxgk.dao.SchoolTermDao;
import io.renren.modules.jxgk.entity.SchoolTermEntity;
import io.renren.modules.jxgk.service.SchoolTermService;
import io.renren.modules.jxgkphone.controller.CourseDateController;
import io.renren.modules.jxgkphone.dao.CourseDateMapper;
import io.renren.modules.jxgkquestion.entity.ScoreDataEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class SchoolTermServiceImpl implements SchoolTermService{
    @Autowired
    SchoolTermDao schoolTermDao;
    @Autowired
    CourseDateController courseDateService;
    @Autowired
    CourseDateMapper courseDateMapper;
    public List<SchoolTermEntity> queryList(Map<String, Object> map){
        return schoolTermDao.queryList(map);
    }
    public int queryTotal(Map<String, Object> map){
        return  schoolTermDao.queryTotal(map);
    }
    public void deleteBatch(String[] termIds){
        schoolTermDao.deleteBatch(termIds);
    }
    public void save(SchoolTermEntity ste){
        //自动补全年份
        if(ste.getSchoolAcademicYear().length()==4){
            int year=Integer.parseInt(ste.getSchoolAcademicYear());
            int afterYear=year+1;
            String fullYear=year+"-"+afterYear;
            ste.setSchoolAcademicYear(fullYear);
        }
        //如果设置为当前学期
        if(ste.getCurrentTerm().equals("1")){
            //若为当前学期 初始化教学周日期对应表
            Map<String,Object> params=new HashMap<>();
            params.put("beginDate",ste.getStartDate());
            params.put("endDate",ste.getEndDate());
            params.put("term",ste.getSchoolAcademicTerm());
            params.put("update","false");
            courseDateService.insert(params);
            //当前学期只能有一个
            List<SchoolTermEntity> currentData=schoolTermDao.getDataList();
            for(SchoolTermEntity s:currentData){
                s.setCurrentTerm("0");//把其他的统统设置为0
                schoolTermDao.update(s);//保存
            }
        }
        schoolTermDao.save(ste);
    }
    public SchoolTermEntity queryObject(String index){
        return schoolTermDao.queryObject(index);
    }
    public void update(SchoolTermEntity ste){
        //自动补全年份
        if(ste.getSchoolAcademicYear().length()==4){
            int year=Integer.parseInt(ste.getSchoolAcademicYear());
            int afterYear=year+1;
            String fullYear=year+"-"+afterYear;
            ste.setSchoolAcademicYear(fullYear);
        }
        //如果设置为当前学期
        if(ste.getCurrentTerm().equals("1")){
            //删除之前的教学周日期对应表
            courseDateMapper.deleteByYearAndTerm(ste.getSchoolAcademicYear(),ste.getSchoolAcademicTerm());
            //添加新的教学周日期对应表
            Map<String,Object> params=new HashMap<>();
            params.put("beginDate",ste.getStartDate());
            params.put("endDate",ste.getEndDate());
            params.put("term",ste.getSchoolAcademicTerm());
            courseDateService.insert(params);
            //当前学期只能有一个
            List<SchoolTermEntity> currentData=schoolTermDao.getDataList();
            for(SchoolTermEntity s:currentData){
                s.setCurrentTerm("0");//把其他的统统设置为0
                schoolTermDao.update(s);//保存
            }
        }
        schoolTermDao.update(ste);
    }

    @Override
    public List<SchoolTermEntity> getSchoolTerm() {
        return schoolTermDao.getSchoolTerm();
    }
}
